package com.snail.billing.page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.util.ResUtil;
import com.snail.billing.DataCache;
import com.snail.billing.PaymentConst;
import com.snail.billing.PaymentParams;
import com.snail.billing.Resource;
import com.snail.billing.json.JsonState;
import com.snail.billing.os.DialogPage;
import com.snail.billing.session.StatePaymentSession;

public class PaymentStatePage extends DialogPage implements View.OnClickListener, AdapterView.OnItemClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private ListView listView;
	private SimpleAdapter adapter;
	private List<Map<String, Object>> data;

	private HttpApp httpApp;

	private StatePaymentSession stateSession;
	private JsonState jsonState;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_payment_state_activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_payment_state_title));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		listView = (ListView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_payment_state_list_view));
		listView.setOnItemClickListener(this);
		//
		httpApp = new HttpApp(getContext());
		httpApp.setOnHttpResultListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (jsonState != null && jsonState.getPlatforms() != null) {
			listView.setAdapter(createAdapter());
		} else {
			stateSession = new StatePaymentSession();
			httpApp.request(stateSession);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		String index = jsonState.getIndexs()[position];
		int platformId = jsonState.getPlatforms().get(index).getPlatformId();
		String platformName = jsonState.getPlatforms().get(index).getName();
		DataCache.getInstance().paymentParams = new PaymentParams();
		DataCache.getInstance().paymentParams.platformId = platformId;
		DataCache.getInstance().paymentParams.platformName = platformName;
		getPageManager().forward(PaymentOrderPage.class);
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(stateSession)) {
				jsonState = new JsonState((String) httpSession.getResponseData());
				if (jsonState != null && jsonState.getPlatforms() != null) {
					listView.setAdapter(createAdapter());
				}
			}
		}
	}

	private SimpleAdapter createAdapter() {
		data = new ArrayList<Map<String, Object>>();
		for (String index : jsonState.getIndexs()) {
			JsonState.Platform platform = jsonState.getPlatforms().get(index);
			Map<String, Object> map = new HashMap<String, Object>();
			int drawableId = ResUtil.getDrawableId(PaymentConst.IMAGE.get(platform.getPlatformId()));
			if (drawableId == 0) {
				drawableId = ResUtil.getDrawableId(Resource.drawable.snailbilling_pay_empty);
			}
			map.put("image", drawableId);
			map.put("text", platform.getName());
			data.add(map);
		}
		//
		int resource = ResUtil.getLayoutId(Resource.layout.snailbilling_payment_state_item);
		String[] from = { "image", "text" };
		int[] to = { ResUtil.getViewId(Resource.id.snailbilling_state_payment_image), ResUtil.getViewId(Resource.id.snailbilling_state_payment_text) };
		adapter = new SimpleAdapter(getContext(), data, resource, from, to);
		return adapter;
	}

}
